% exp/ilp2008/ilp2008_exp.pro
%
% Experiments for ILP2008 paper
%

% consult configuration file
:- (getenv('KERNEL_CONFIG',C) ; C='../../config'), [C].

:- use_module(kernel_lib(kernel)).
:- use_module(kernel_lib(parse)).
:- use_module(kernel_lib(file)).
:- use_module(kernel_lib(write_basic_term)).
:- use_module(kernel_lib(basic_term)).

:- dynamic
        id_ix/2.


:- [
        kernel_data('cora-ref/recleaned-all-labeled.pro'),
        kernel_dcg('ilp2008_cora-ref_dcg1.pro')
   ].


parse(cora) :-
    forall(
        data(Id,Authors,Title,Journal,_Volume,Tech,Booktitle,_Editor,Publisher,Institution,Address,_Pages,_Note,Type,_Date,Year,_Month),
        (
            % no venue field in cora, so create one by combining relevant fields
            Fs = [Journal,Tech,Booktitle,Publisher,Institution,Address,Type],
            findall(F, (member(F,Fs), F\==''), Fields),
            concat_atom(Fields, ' ', Venue),

            parse_term(
                data(Authors,Title,Venue,Year),
                TypedTerm
            ),
            E =.. [Id, TypedTerm::individual],
            assert(E::class)
        )
    ).


write_typed1 :-
    pathfile(kernel_data, 'cora-ref/ilp2008_cora-ref_typed1.pro', FileName),
    tell(FileName),
    listing(_::class),
    told.

write_typed2 :-
    findall(T, T::class, Ts),
    retractall(_::class),
    forall(
        member(I,Ts),
        (
            I =.. [Class,X],
            basic_term_project(X, '11', Y), % set of authors
            Y = Z::set,
            J =.. [Class,Z::authors],
            assert(J::class)
        )
    ),
    pathfile(kernel_data, 'cora-ref/ilp2008_cora-ref_typed2.pro', FileName),
    tell(FileName),
    listing(_::class),
    told.

write_typed3 :-
    findall(T, T::class, Ts),
    retractall(_::class),
    forall(
        member(I,Ts),
        (
            I =.. [Class,X],
            basic_term_project(X, '2', Y), % singleton set of title
            list_to_basic_set(Ys,Y),
            Ys = [Title],
            J =.. [Class,Title],
            assert(J::class)
        )
    ),
    pathfile(kernel_data, 'cora-ref/ilp2008_cora-ref_typed3.pro', FileName),
    tell(FileName),
    listing(_::class),
    told.

write_typed4 :-
    findall(T, T::class, Ts),
    retractall(_::class),
    forall(
        member(I,Ts),
        (
            I =.. [Class,X],
            basic_term_project(X, '3', Y), % singleton set of venue
            list_to_basic_set(Ys,Y),
            Ys = [Venue],
            J =.. [Class,Venue],
            assert(J::class)
        )
    ),
    pathfile(kernel_data, 'cora-ref/ilp2008_cora-ref_typed4.pro', FileName),
    tell(FileName),
    listing(_::class),
    told.


labels1 :-
    pathfile(kernel_out, 'cora-ref/ilp2008_cora-ref_labels1.csv', LabelsFile),
    tell(LabelsFile),
    forall(
        data(Id,Author,Title,Journal,Volume,Tech,Booktitle,Editor,
             Publisher,Institution,Address,Pages,Note,Type,Date,Year,Month),
        (
            _L = [Author,Title,Journal,Volume,Tech,Booktitle,Editor,
                 Publisher,Institution,Address,Pages,Note,Type,Date,Year,Month],
%            format('~q - ~q\n', [Id,_L])
            format('~q\n', [Id])
        )
    ),
    told.



truth1 :-
    % write class number of each data item
    pathfile(kernel_out, 'cora-ref/ilp2008_cora-ref_truth1.csv', File),
    tell(File),
    findall(Id, data(Id,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_,_), Ids),
    data_class_reset,
    forall(
        member(Id,Ids),
        (
            data_class(Id,ClassNumber),
            format('~w\n', [ClassNumber])
        )
    ),
    told.

data_class_reset :-
    retractall(id_ix(_,_)),
    flag(class_number, _, 1).
%   deterministically assign/return class number for a given class name
%   NB. must invoke data_class_reset before using data_class
data_class(ClassName,ClassNumber) :-
    (id_ix(ClassName,ClassNumber)
    ->  true
    ;   (
            flag(class_number, ClassNumber, ClassNumber+1),
            assert(id_ix(ClassName,ClassNumber))
        )
    ).


experiment1 :-
    consult(kernel_data('cora-ref/ilp2008_cora-ref_typed1.pro')),
    findall(D,(X::class,X=..[_,D]),Xs),
    pathfile(kernel_out, 'cora-ref/ilp2008_cora-ref_kbtU1.csv', KernelFile),
    tell(KernelFile),
    symmetric_cross_product_write(kernel(basic_terms),Xs),
    told.

experiment2 :-
    consult(kernel_data('cora-ref/ilp2008_cora-ref_typed2.pro')),
    findall(D,(X::class,X=..[_,D]),Xs),
    pathfile(kernel_out, 'cora-ref/ilp2008_cora-ref_kbtU2.csv', KernelFile),
    tell(KernelFile),
    symmetric_cross_product_write(kernel(basic_terms),Xs),
    told.

experiment3 :-
    consult(kernel_data('cora-ref/ilp2008_cora-ref_typed3.pro')),
    findall(D,(X::class,X=..[_,D]),Xs),
    pathfile(kernel_out, 'cora-ref/ilp2008_cora-ref_kbtU3.csv', KernelFile),
    tell(KernelFile),
    symmetric_cross_product_write(kernel(basic_terms),Xs),
    told.

experiment4 :-
    consult(kernel_data('cora-ref/ilp2008_cora-ref_typed4.pro')),
    findall(D,(X::class,X=..[_,D]),Xs),
    pathfile(kernel_out, 'cora-ref/ilp2008_cora-ref_kbtU4.csv', KernelFile),
    tell(KernelFile),
    symmetric_cross_product_write(kernel(basic_terms),Xs),
    told.
